<template>
  <!-- element-ui级联组件 -->
  <el-cascader
    :value="value"
    size="mini"
    :options="treeData"
    :props="propsSelcet"
    separator="-"
    @change="changeValue"
  />
</template>
<script>
import { getDepartment } from '@/api/department'
import { transListToTreeData } from '@/utils'
export default {
  props: {
    value: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      treeData: [],
      propsSelcet: {
        label: 'name',
        value: 'id'
      }
    }
  },
  created() {
    this.getDepartmentList()
  },
  methods: {
    async getDepartmentList() {
      const res = await getDepartment()
      this.treeData = transListToTreeData(res, 0)
    },
    changeValue(list) {
      // console.log(list)
      if (list.length > 0) {
        this.$emit('input', list[list.length - 1])
      } else {
        this.$emit('input', null)
      }
    }
  }
}
</script>
<style lang="scss" scoped>

</style>
